package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private FeeKeyMapper feeKeyMapper;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    @Transactional
    public void saveOrUpdateAll(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isSave = apartmentSubmitVo.getId() == null;
        super.saveOrUpdate(apartmentSubmitVo);

        if(!isSave){// 更新先全部删除
            // 删除公寓图片
            LambdaQueryWrapper<GraphInfo> graphInfoQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoQueryWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoQueryWrapper);

            // 删除配套列表
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityQueryWrapper.eq(ApartmentFacility::getApartmentId, apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityQueryWrapper);

            // 删除标签列表
            LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelQueryWrapper);

            // 删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
            feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeQueryWrapper);
        }

        if(apartmentSubmitVo.getGraphVoList() != null) {
            // 插入公寓图片
            List<GraphInfo> graphInfos = apartmentSubmitVo.getGraphVoList().stream().map(graphVo -> {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setItemType(ItemType.APARTMENT);
                BeanUtils.copyProperties(graphVo, graphInfo);
                return graphInfo;
            }).toList();
            graphInfoService.saveBatch(graphInfos);
        }

        if(apartmentSubmitVo.getFacilityInfoIds() != null) {
            // 插入配套列表
            List<ApartmentFacility> apartmentFacilities = apartmentSubmitVo.getFacilityInfoIds()
                    .stream().map(facilityInfoId -> ApartmentFacility.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .facilityId(facilityInfoId)
                    .build()).toList();
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }

        if(apartmentSubmitVo.getLabelIds() != null) {
            // 插入标签列表
            List<ApartmentLabel> apartmentLabels = apartmentSubmitVo.getLabelIds()
                    .stream().map(labelId -> ApartmentLabel.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .labelId(labelId)
                    .build()).toList();
            apartmentLabelService.saveBatch(apartmentLabels);
        }

        if(apartmentSubmitVo.getFeeValueIds() != null) {
            // 插入杂费列表
            List<ApartmentFeeValue> apartmentFeeValues = apartmentSubmitVo.getFeeValueIds()
                    .stream().map(feeValueId -> ApartmentFeeValue.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .feeValueId(feeValueId)
                    .build()).toList();
            apartmentFeeValueService.saveBatch(apartmentFeeValues);
        }
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();

        // 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        // 查询图片列表
        LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
        graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoWrapper.eq(GraphInfo::getItemId, id);
        List<GraphInfo> graphInfos = graphInfoService.list(graphInfoWrapper);
        if(!graphInfos.isEmpty()) {
            apartmentDetailVo.setGraphVoList(graphInfos.stream().map(graphInfo -> {
                GraphVo graphVo = new GraphVo();
                BeanUtils.copyProperties(graphInfo, graphVo);
                return graphVo;
            }).toList());
        }

        // 查询标签列表
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelWrapper = new LambdaQueryWrapper<>();
        apartmentLabelWrapper.eq(ApartmentLabel::getApartmentId, id);
        List<ApartmentLabel> apartmentLabels = apartmentLabelService.list(apartmentLabelWrapper);
        if(!apartmentLabels.isEmpty()) {
            apartmentDetailVo.setLabelInfoList(apartmentLabels.stream()
                    .map(apartmentLabel -> labelInfoMapper.selectById(apartmentLabel.getLabelId())).toList());
        }

        // 查询配套列表
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityWrapper.eq(ApartmentFacility::getApartmentId, id);
        List<ApartmentFacility> apartmentFacilities = apartmentFacilityService.list(apartmentFacilityWrapper);
        if(!apartmentFacilities.isEmpty()) {
            apartmentDetailVo.setFacilityInfoList(apartmentFacilities.stream()
                    .map(apartmentFacility -> facilityInfoMapper.selectById(apartmentFacility.getFacilityId())).toList());
        }

        // 查询杂费列表
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        List<ApartmentFeeValue> apartmentFeeValues = apartmentFeeValueService.list(apartmentFeeValueWrapper);
        if(!apartmentFeeValues.isEmpty()) {
            apartmentDetailVo.setFeeValueVoList(apartmentFeeValues.stream().map(apartmentFeeValue -> {
                FeeValue feeValue = feeValueMapper.selectById(apartmentFeeValue.getFeeValueId());
                FeeValueVo feeValueVo = new FeeValueVo();
                BeanUtils.copyProperties(feeValue, feeValueVo);
                FeeKey feeKey = feeKeyMapper.selectById(feeValue.getFeeKeyId());
                feeValueVo.setFeeKeyName(feeKey.getName());
                return feeValueVo;
            }).toList());
        }
        return apartmentDetailVo;
    }

    @Override
    @Transactional
    public void removeApartmentById(Long id) {

        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
            //直接为前端返回如下响应：先删除房间信息再删除公寓信息
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
        }

        super.removeById(id);

        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelQueryWrapper);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityQueryWrapper);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeQueryWrapper);
    }
}




